package com.kn.sys.controller;

import com.kn.common.model.PageBean;
import com.kn.common.model.R;
import com.kn.common.valid.AddGroup;
import com.kn.common.valid.EditGroup;
import com.kn.sys.dto.UserDto;
import com.kn.sys.service.UserService;
import lombok.RequiredArgsConstructor;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.constraints.NotBlank;

/**
 * @author zhouxp
 * @create 2021-03-04 11:47
 */
@RestController
@RequestMapping("sys/user")
@RequiredArgsConstructor
@CrossOrigin
public class UserController {
   private final UserService userService;

   @GetMapping
   @RequiresPermissions("sys:user:list")
   public R list(PageBean pageBean,String name){
      return userService.list(pageBean,name);
   }

   @PostMapping
   public R add(@Validated(AddGroup.class) @RequestBody UserDto userDto){
      return userService.add(userDto);
   }
@DeleteMapping("{id}")
   public R delete(@PathVariable Long id){
      return userService.delete(id);
}
@PutMapping("{id}")
public R edit(@PathVariable Long id,@Validated(EditGroup.class)@RequestBody UserDto userDto){
   return userService.edit(id,userDto);
}
@GetMapping("/userid/{userid}")
   public R getRoleIdByUserId(@PathVariable Long userid){
      return userService.getRoleIdByUserId(userid);
}
@GetMapping("/login")
   public R login(@NotBlank(message = "账号必填") String username,@NotBlank(message = "密码必填") String password) {
      return userService.login(username,password);
   }
}